public class SolutionOffer22 {
    public static class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
    }
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode tail=head;
        for (int i = 0; i < k - 1; i++) {
            tail=tail.next;
        }
        while (tail.next!=null){
            tail=tail.next;
            head=head.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode last=null,now=null;
        for (int i=5;i>=1;i--){
            now=new ListNode(i);
            now.next=last;
            last=now;
        }
        System.out.println(new SolutionOffer22().getKthFromEnd(now,2).val);
    }
}
